Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Variable-length array</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Variable-length_array"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Variable-length_array rootpage-Variable-length_array skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Variable-length array</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>variable-length array</b> (<b>VLA</b>), also called <b>variable-sized</b> or <b>runtime-sized</b>, is an <a href="Array_data_structure" class="mw-redirect" title="Array data structure">array data structure</a> whose length is determined at <a href="Execution_(computing)#Runtime" title="Execution (computing)">runtime</a>, instead of at <a href="Compile_time" title="Compile time">compile time</a>.<sup id="cite_ref-cray_1-0" class="reference"><a href="#cite_note-cray-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> In the language <a href="C_(programming_language)" title="C (programming language)">C</a>, the VLA is said to have a variably modified <a href="Data_type" title="Data type">data type</a> that depends on a value (see <a href="Dependent_type" title="Dependent type">Dependent type</a>).
</p><p>The main purpose of VLAs is to simplify programming of <a href="Numerical_method" title="Numerical method">numerical algorithms</a>.
</p><p>Programming languages that support VLAs include <a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>, <a href="ALGOL_68" title="ALGOL 68">ALGOL 68</a> (for non-flexible rows), <a href="APL_(programming_language)" title="APL (programming language)">APL</a>, <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a> (as unsafe-mode <a href="Stack-based_memory_allocation" title="Stack-based memory allocation">stack-allocated</a> arrays), <a href="COBOL" title="COBOL">COBOL</a>, <a href="Fortran" title="Fortran">Fortran</a> 90, <a href="J_(programming_language)" title="J (programming language)">J</a>, and <a href="Object_Pascal" title="Object Pascal">Object Pascal</a> (the language used in <a href="Delphi_(software)" title="Delphi (software)">Delphi</a> and <a href="Lazarus_(software)" title="Lazarus (software)">Lazarus</a>, that uses FPC). <a href="C99" title="C99">C99</a> introduced support for VLAs, although they were subsequently relegated in <a href="C11_(C_standard_revision)" title="C11 (C standard revision)">C11</a> to a conditional feature, which implementations are not required to support;<sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> on some platforms, VLAs could be implemented formerly with <code><a href="Alloca" class="mw-redirect" title="Alloca">alloca</a>()</code> or similar functions.
</p><p>Growable arrays (also called <a href="Dynamic_array" title="Dynamic array">dynamic arrays</a>) are generally more useful than VLAs because dynamic arrays can do everything VLAs can do, and also support growing the array at run-time. For this reason, many programming languages (<a href="JavaScript" title="JavaScript">JavaScript</a>, <a href="Java_(programming_language)" title="Java (programming language)">Java</a>, <a href="Python_(programming_language)" title="Python (programming language)">Python</a>, <a href="R_(programming_language)" title="R (programming language)">R</a>, etc.) only support growable arrays. Even in languages that support variable-length arrays, it's often recommended to avoid using (stack-based) variable-length arrays, and instead use (<a href="Heap_(data_structure)" title="Heap (data structure)">heap-based</a>) dynamic arrays.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Memory">Memory</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Allocation">Allocation</h3></div>
<ul><li>The <a href="GNU_Compiler_Collection" title="GNU Compiler Collection">GNU Compiler Collection</a> (GCC) for C allocates memory for VLAs with <a href="Automatic_variable" title="Automatic variable">automatic storage duration</a> on the <a href="Stack-based_memory_allocation" title="Stack-based memory allocation">stack</a>.<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> This is the faster and more straightforward option compared to heap-allocation, and is used by most compilers.</li>
<li>VLAs can also be allocated on the <a href="Memory_management#Manual_memory_management" title="Memory management">heap</a> and internally accessed using a pointer to this block.</li></ul>
<div class="mw-heading mw-heading2"><h2 id="Implementation">Implementation</h2></div>
<div class="mw-heading mw-heading3"><h3 id="C99">C99</h3></div>
<p>The following <a href="C99" title="C99">C99</a> function allocates a variable-length array of a specified size, fills it with floating-point values, and then passes it to another function for processing. Because the array is declared as an automatic variable, its lifetime ends when <code>read_and_process()</code> returns.
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">float</span><span class="w"> </span><span class="nf">read_and_process</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">n</span><span class="p">];</span>

<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span>
<span class="w"> </span><span class="n">vals</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">read_val</span><span class="p">();</span>

<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">process</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="n">vals</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>In C99, the length parameter must come before the variable-length array parameter in function calls.<sup id="cite_ref-cray_1-1" class="reference"><a href="#cite_note-cray-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> In C11, a <code class="mw-highlight mw-highlight-lang-text mw-content-ltr" style="" dir="ltr">__STDC_NO_VLA__</code> macro is defined if VLA is not supported.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup> The C23 standard makes VLA types mandatory again. Only creation of VLA objects with automatic storage duration is optional.<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> GCC had VLA as an extension before C99, one that also extends into its C++ dialect.
</p><p><a href="Linus_Torvalds" title="Linus Torvalds">Linus Torvalds</a> has expressed his displeasure in the past over VLA usage for arrays with predetermined small sizes because it generates lower quality assembly code.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> With the Linux 4.20 kernel, the <a href="Linux_kernel" title="Linux kernel">Linux kernel</a> is effectively VLA-free.<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup>
</p><p>Although C11 does not explicitly name a size-limit for VLAs, some believe it should have the same maximum size as all other objects, i.e. SIZE_MAX bytes.<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> However, this should be understood in the wider context of environment and platform limits, such as the typical stack-guard page size of 4&nbsp;KiB, which is many orders of magnitude smaller than SIZE_MAX.
</p><p>It is possible to have VLA object with dynamic storage by using a pointer to an array.
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">float</span><span class="w"> </span><span class="nf">read_and_process</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">n</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">vals</span><span class="p">)[</span><span class="n">n</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="kt">float</span><span class="p">[</span><span class="n">n</span><span class="p">]));</span>

<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">n</span><span class="p">;</span><span class="w"> </span><span class="o">++</span><span class="n">i</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="o">*</span><span class="n">vals</span><span class="p">)[</span><span class="n">i</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">read_val</span><span class="p">();</span>

<span class="w"> </span><span class="kt">float</span><span class="w"> </span><span class="n">ret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">process</span><span class="p">(</span><span class="n">n</span><span class="p">,</span><span class="w"> </span><span class="o">*</span><span class="n">vals</span><span class="p">);</span>
<span class="w"> </span>
<span class="w"> </span><span class="n">free</span><span class="p">(</span><span class="n">vals</span><span class="p">);</span>
<span class="w"> </span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">ret</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Ada">Ada</h3></div>
<p>The following is the same example in <a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>. Ada arrays carry their bounds with them, so there is no need to pass the length to the Process function.
</p>
<div class="mw-highlight mw-highlight-lang-ada mw-content-ltr" dir="ltr"><pre><span class="kd">type</span> <span class="kt">Vals_Type</span> <span class="kr">is</span> <span class="kr">array</span> <span class="p">(</span><span class="kt">Positive</span> <span class="kr">range</span> <span class="p">&lt;&gt;)</span> <span class="kr">of</span> <span class="kt">Float</span><span class="p">;</span>

<span class="kd">function</span> <span class="nf">Read_And_Process</span> <span class="p">(</span><span class="nv">N</span> <span class="p">: </span><span class="nv">Integer</span><span class="p">)</span> <span class="kr">return</span> <span class="kt">Float</span> <span class="kr">is</span>
<span class="n">Vals</span> <span class="p">:</span> <span class="n">Vals_Type</span> <span class="p">(</span><span class="mi">1</span> <span class="p">..</span> <span class="n">N</span><span class="p">);</span>
<span class="kr">begin</span>
<span class="kr">for</span> <span class="n">I</span> <span class="ow">in</span> <span class="mi">1</span> <span class="p">..</span> <span class="n">N</span> <span class="kr">loop</span>
<span class="n">Vals</span> <span class="p">(</span><span class="n">I</span><span class="p">)</span> <span class="p">:=</span> <span class="n">Read_Val</span><span class="p">;</span>
<span class="kr">end</span> <span class="kr">loop</span><span class="p">;</span>
<span class="kr">return</span> <span class="n">Process</span> <span class="p">(</span><span class="n">Vals</span><span class="p">);</span>
<span class="kr">end</span> <span class="nf">Read_And_Process</span><span class="p">;</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Fortran_90">Fortran 90</h3></div>
<p>The equivalent <a href="Fortran" title="Fortran">Fortran 90</a> function is
</p>
<div class="mw-highlight mw-highlight-lang-fortran mw-content-ltr" dir="ltr"><pre><span class="k">function </span><span class="n">read_and_process</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="k">result</span><span class="p">(</span><span class="n">o</span><span class="p">)</span>
<span class="w"> </span><span class="kt">integer</span><span class="p">,</span><span class="k">intent</span><span class="p">(</span><span class="n">in</span><span class="p">)</span><span class="kd">::</span><span class="n">n</span>
<span class="w"> </span><span class="kt">real</span><span class="kd">::</span><span class="n">o</span>

<span class="w"> </span><span class="kt">real</span><span class="p">,</span><span class="k">dimension</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="kd">::</span><span class="n">vals</span>
<span class="w"> </span><span class="kt">integer</span><span class="kd">::</span><span class="n">i</span>

<span class="w"> </span><span class="k">do </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="n">n</span>
<span class="w"> </span><span class="n">vals</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">read_val</span><span class="p">()</span>
<span class="w"> </span><span class="k">end do</span>
<span class="k"> </span><span class="n">o</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">process</span><span class="p">(</span><span class="n">vals</span><span class="p">)</span>
<span class="k">end function </span><span class="n">read_and_process</span>
</pre></div>
<p>when utilizing the Fortran 90 feature of checking procedure interfaces at compile time; on the other hand, if the functions use pre-Fortran 90 call interface, the (external) functions must first be declared, and the array length must be explicitly passed as an argument (as in C):
</p>
<div class="mw-highlight mw-highlight-lang-fortran mw-content-ltr" dir="ltr"><pre><span class="k">function </span><span class="n">read_and_process</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="w"> </span><span class="k">result</span><span class="p">(</span><span class="n">o</span><span class="p">)</span>
<span class="w"> </span><span class="kt">integer</span><span class="p">,</span><span class="k">intent</span><span class="p">(</span><span class="n">in</span><span class="p">)</span><span class="kd">::</span><span class="n">n</span>
<span class="w"> </span><span class="kt">real</span><span class="kd">::</span><span class="n">o</span>

<span class="w"> </span><span class="kt">real</span><span class="p">,</span><span class="k">dimension</span><span class="p">(</span><span class="n">n</span><span class="p">)</span><span class="kd">::</span><span class="n">vals</span>
<span class="w"> </span><span class="kt">real</span><span class="kd">::</span><span class="n">read_val</span><span class="p">,</span><span class="w"> </span><span class="n">process</span>
<span class="w"> </span><span class="kt">integer</span><span class="kd">::</span><span class="n">i</span>

<span class="w"> </span><span class="k">do </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="n">n</span>
<span class="w"> </span><span class="n">vals</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">read_val</span><span class="p">()</span>
<span class="w"> </span><span class="k">end do</span>
<span class="k"> </span><span class="n">o</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">process</span><span class="p">(</span><span class="n">vals</span><span class="p">,</span><span class="n">n</span><span class="p">)</span>
<span class="k">end function </span><span class="n">read_and_process</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Cobol">Cobol</h3></div>
<p>The following <a href="COBOL" title="COBOL">COBOL</a> fragment declares a variable-length array of records <code>DEPT-PERSON</code> having a length (number of members) specified by the value of <code>PEOPLE-CNT</code>:
</p>
<div class="mw-highlight mw-highlight-lang-cobolfree mw-content-ltr" dir="ltr"><pre><span class="kr">DATA</span><span class="w"> </span><span class="kr">DIVISION</span><span class="p">.</span>
<span class="kr">WORKING-STORAGE</span><span class="w"> </span><span class="kr">SECTION</span><span class="p">.</span>
<span class="mi">01 </span><span class="nv">DEPT-PEOPLE</span><span class="p">.</span>
<span class="w"> </span><span class="mi">05 </span><span class="nv">PEOPLE-CNT</span><span class="w"> </span><span class="kt">PIC S9(4)</span><span class="w"> </span><span class="kt">BINARY</span><span class="p">.</span>
<span class="w"> </span><span class="mi">05 </span><span class="nv">DEPT-PERSON</span><span class="w"> </span><span class="kp">OCCURS</span><span class="w"> </span><span class="mi">0 </span><span class="kp">TO</span><span class="w"> </span><span class="mi">20 </span><span class="kp">TIMES</span><span class="w"> </span><span class="kp">DEPENDING</span><span class="w"> </span><span class="kp">ON</span><span class="w"> </span><span class="nv">PEOPLE-CNT</span><span class="p">.</span>
<span class="w"> </span><span class="mi">10 </span><span class="nv">PERSON-NAME</span><span class="w"> </span><span class="kt">PIC X(20)</span><span class="p">.</span>
<span class="w"> </span><span class="mi">10 </span><span class="nv">PERSON-WAGE</span><span class="w"> </span><span class="kt">PIC S9(7)V99</span><span class="w"> </span><span class="kp">PACKED-DECIMAL</span><span class="p">.</span>
</pre></div>
<p>The <a href="COBOL" title="COBOL">COBOL</a> VLA, unlike that of other languages mentioned here, is safe because COBOL requires specifying maximum array size. In this example, <code>DEPT-PERSON</code> cannot have more than 20 items, regardless of the value of <code>PEOPLE-CNT</code>.
</p>
<div class="mw-heading mw-heading3"><h3 id="C#">C#</h3></div>
<p>The following <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a> fragment declares a variable-length array of integers. Before C# version 7.2, a pointer to the array is required, requiring an "unsafe" context. The "unsafe" keyword requires an assembly containing this code to be marked as unsafe.
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="k">unsafe</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">DeclareStackBasedArrayUnsafe</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="kt">int</span><span class="o">*</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">stackalloc</span><span class="w"> </span><span class="kt">int</span><span class="p">[</span><span class="n">size</span><span class="p">];</span>
<span class="w"> </span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">123</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>C# version 7.2 and later allow the array to be allocated without the "unsafe" keyword, through the use of the Span feature.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="k">void</span><span class="w"> </span><span class="nf">DeclareStackBasedArraySafe</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">size</span><span class="p">)</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">Span</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">stackalloc</span><span class="w"> </span><span class="kt">int</span><span class="p">[</span><span class="n">size</span><span class="p">];</span>
<span class="w"> </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">123</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Object_Pascal">Object Pascal</h3></div>
<p><a href="Object_Pascal" title="Object Pascal">Object Pascal</a> dynamic arrays are allocated on the heap.<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
</p><p>In this language, it is called a dynamic array. The declaration of such a variable is similar to the declaration of a static array, but without specifying its size. The size of the array is given at the time of its use.
</p>
<div class="mw-highlight mw-highlight-lang-delphi mw-content-ltr" dir="ltr"><pre><span class="k">program</span><span class="w"> </span><span class="n">CreateDynamicArrayOfNumbers</span><span class="p">(</span><span class="n">Size</span><span class="o">:</span><span class="w"> </span><span class="kt">Integer</span><span class="p">)</span><span class="o">;</span>
<span class="k">var</span>
<span class="w"> </span><span class="n">NumberArray</span><span class="o">:</span><span class="w"> </span><span class="k">array</span><span class="w"> </span><span class="k">of</span><span class="w"> </span><span class="kt">LongWord</span><span class="o">;</span>
<span class="k">begin</span>
<span class="w"> </span><span class="nb">SetLength</span><span class="p">(</span><span class="n">NumberArray</span><span class="o">,</span><span class="w"> </span><span class="n">Size</span><span class="p">)</span><span class="o">;</span>
<span class="w"> </span><span class="n">NumberArray</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="mi">2020</span><span class="o">;</span>
<span class="k">end</span><span class="o">.</span>
</pre></div>
<p>Removing the contents of a dynamic array is done by assigning it a size of zero.
</p>
<div class="mw-highlight mw-highlight-lang-delphi mw-content-ltr" dir="ltr"><pre><span class="o">...</span>
<span class="n">SetLength</span><span class="p">(</span><span class="n">NumberArray</span><span class="o">,</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="o">;</span>
<span class="o">...</span>
</pre></div>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-cray-1"><span class="mw-cite-backlink">^ <a href="#cite_ref-cray_1-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-cray_1-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20180126153326/http://docs.cray.com/books/004-2179-001/html-004-2179-001/z893434830malz.html">"Variable Length Arrays"</a>. Archived from <a rel="nofollow" class="external text" href="http://docs.cray.com/books/004-2179-001/html-004-2179-001/z893434830malz.html">the original</a> on 2018-01-26.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html">"Variable Length – Using the GNU Compiler Collection (GCC)"</a>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text">ISO 9899:2011 Programming Languages – C 6.7.6.2&nbsp;4.</span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="CITEREFRaymond2000" class="citation web cs1">Raymond, Eric S. (2000). <a rel="nofollow" class="external text" href="https://tldp.org/HOWTO/Software-Release-Practice-HOWTO/develpractice.html">"Raymond Software Release Practice Howto: 6. Good development practice"</a>. <i>The Linux Documentation Project</i>.</cite></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://gcc.gnu.org/onlinedocs/gfortran/Code-Gen-Options.html">"Code Gen Options - The GNU Fortran Compiler"</a>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text">§ 6.10.8.3 of the C11 standard (n1570.pdf)</span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text">§ 6.10.9.3 of the C23 standard (n3054.pdf)</span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite id="CITEREFTorvalds2018" class="citation mailinglist cs1"><a href="Linus_Torvalds" title="Linus Torvalds">Torvalds, Linus</a> (7 March 2018). <a rel="nofollow" class="external text" href="https://lkml.org/lkml/2018/3/7/621">"LKML: Linus Torvalds: Re: VLA removal (was Re: [RFC 2/2] lustre: use VLA_SAFE)"</a>. <i>Linux kernel</i> (Mailing list).</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.phoronix.com/scan.php?page=news_item&amp;px=Linux-Kills-The-VLA">"The Linux Kernel Is Now VLA-Free: A Win For Security, Less Overhead &amp; Better For Clang - Phoronix"</a>. <i>www.phoronix.com</i>.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text">§6.5.3.4 and §7.20.3 of the C11 standard (n1570.pdf)</span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/stackalloc">"stackalloc operator (C# reference)"</a>. Microsoft. 10 July 2024.</cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text">
Michaël Van Canneyt.
<a rel="nofollow" class="external text" href="https://www.freepascal.org/docs-html/ref/refsu14.html#x38-520003.3.1">"Free Pascal Reference guide: Dynamic arrays"</a>.</span>
</li>
</ol></div></div></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-08-06" href="https://en.wikipedia.org/wiki/?title=Variable-length_array&amp;oldid=1304450677">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>